package cn.enited.devtools.apiaggregator.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Accessors(chain = true)
public class SetActionResult<T> extends ActionResult {
    private static final long serialVersionUID = 1985354947968184950L;

    private Set<T> data;

    public SetActionResult() {
        this.data = new HashSet<>();
    }

    public SetActionResult(int code, String message) {
        super(code, message);
        this.data = new HashSet<>();
    }

    private SetActionResult(int code, String message, Set<T> data) {
        super(code, message);
        this.data = Objects.isNull(data) ? new HashSet<>() : data;
    }

    public void add(T item) {
        this.data.add(item);
    }

    public void addAll(Collection<T> items) {
        this.data.addAll(items);
    }

    public void clear() {
        this.data.clear();
    }

    public static <T> SetActionResult<T> of(int code, String message, Set<T> data) {
        return new SetActionResult<T>(code, message, data);
    }
}
